package com.changpay.zywy.ui.activity.count;

import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.changpay.zywy.R;
import com.changpay.zywy.ui.dialog.SelectMonthDialog;
import com.changpay.zywy.utils.DateUtil;
import com.changpay.zywy.utils.StringUtil;
import com.ruffian.library.widget.RTextView;
import kale.ui.view.dialog.EasyDialog;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AndroidIntexh1 on 2018/12/20.
 */

public class CountTrandMonthView extends RelativeLayout {

    RelativeLayout rvMore;
    TextView tvTitle;
    ImageView ivMore;
    LinearLayout llContent;


    RTextView tvTimeSearch;


    TextView tv_total_cloud_pay;  // 云支付
    TextView tv_total_card_pay; //  信用卡
    TextView tv_total_bank_card_pay; // 银行卡
    TextView tv_total_fast_pay;  // 快捷支付
    TextView tv_total_scan_pay; // 扫码支付
    TextView tv_trand_total_text; // 交易总数

    TextView tv_total_cloud_times;  // 云支付
    TextView tv_total_card_times; //  信用卡
    TextView tv_total_bank_card_times; // 银行卡
    TextView tv_total_fast_times;  // 快捷支付
    TextView tv_total_scan_times; // 扫码支付
    TextView tv_trand_times_text; // 交易笔数


    PieChartView ct_trand_total;  // 交易量比例饼图
    PieChartView ct_trand_times;  // 交易笔数例饼图

    CountChartActionClose countChartOtherClose;
    public boolean isDisplay = false;


    private View view;
    private DatePickerDialog.OnDateSetListener onDateSetListener;
    private String month;

    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    android.support.v4.app.FragmentManager fragmentManager;

    public CountTrandMonthView(Context context) {
        this(context, null);
    }

    public CountTrandMonthView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        month = DateUtil.INSTANCE.getYearMonth(System.currentTimeMillis());
        view = View.inflate(context, R.layout.count_item_total_tranding, this);

        rvMore = view.findViewById(R.id.rv_more);
        tvTitle = view.findViewById(R.id.tv_title);
        llContent = view.findViewById(R.id.llContent);
        tvTimeSearch =view.findViewById(R.id.tv_time_search);

         tv_total_cloud_pay = view.findViewById(R.id.tv_total_cloud_pay);
         tv_total_card_pay= view.findViewById(R.id.tv_total_card_pay);
         tv_total_bank_card_pay = view.findViewById(R.id.tv_total_bank_card_pay);
         tv_total_fast_pay = view.findViewById(R.id.tv_total_fast_pay);
         tv_total_scan_pay = view.findViewById(R.id.tv_total_scan_pay);
         tv_trand_total_text = view.findViewById(R.id.tv_trand_total_text);

         tv_total_cloud_times = view.findViewById(R.id.tv_total_cloud_times);
         tv_total_card_times = view.findViewById(R.id.tv_total_card_times);
         tv_total_bank_card_times = view.findViewById(R.id.tv_total_bank_card_times);
         tv_total_fast_times = view.findViewById(R.id.tv_total_fast_times);
         tv_total_scan_times = view.findViewById(R.id.tv_total_scan_times);
         tv_trand_times_text = view.findViewById(R.id.tv_trand_times_text);



        ct_trand_total = view.findViewById(R.id.ct_trand_total);
        ct_trand_times = view.findViewById(R.id.ct_trand_times);




        tvTimeSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//                DialogUtils.showDatePickerDialog(context, onDateSetListener, Calendar.getInstance());
                SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(context);
                builder.setOnDialogClickListener((v, month) -> {
                    tv_trand_total_text.setText(month+"总交易量");
                    tv_trand_times_text.setText(month+"总交易笔数");
                    initDateData(month);
                });
                EasyDialog build = builder.build();
                build.setCancelable(true);
                build.show(fragmentManager);
            }
        });

        onDateSetListener = (datePicker, year, month, day) -> {
            String select_date = year+ "" + String.format("%02d", month+1);
            Log.e("mango", "onDateSet: " + select_date);
            initDateData(select_date);
        };
    }

    private void initDateData(String month) {
        countChartOtherClose.loadTrandMonthCharData(month);
    }

    public void setData(double cloudPay, double cardPay, double bankCardPay, double fastPay, double scanPay,
                        int cloudPayTimes, int cardPayTimes, int bankCardPayTimes, int fastPayTimes, int scanPayTimes ) {

        formatFloat(tv_total_cloud_pay,cloudPay,"元");
        formatFloat(tv_total_card_pay,cardPay,"元");
        formatFloat(tv_total_bank_card_pay,bankCardPay,"元");
        formatFloat(tv_total_fast_pay,fastPay,"元");
        formatFloat(tv_total_scan_pay,scanPay,"元");



        formatInt(tv_total_cloud_times,cloudPayTimes,"笔");
        formatInt(tv_total_card_times,cardPayTimes,"笔");
        formatInt(tv_total_bank_card_times,bankCardPayTimes,"笔");
        formatInt(tv_total_fast_times,fastPayTimes,"笔");
        formatInt(tv_total_scan_times,scanPayTimes,"笔");

        initFloatPerChart(ct_trand_total, cloudPay,  cardPay,  bankCardPay,  fastPay,  scanPay);
        initPerChart(ct_trand_times,   cloudPayTimes,  cardPayTimes,  bankCardPayTimes,  fastPayTimes,  scanPayTimes);
    }

    private void initPerChart(PieChartView chart, int h9AddAction, int mp70AddAction, int zhibeijiAction, int h9Action, int mp70Action) {
        PieChartData data;
        int numValues = 5;
        String[] colors = {"#FFF2C1F0", "#FFAEDAFF", "#FFFFC879", "#FF9BEDB8", "#FFFFDCBC"};
        int[] intValues = {h9AddAction, mp70AddAction, zhibeijiAction, h9Action, mp70Action};
        int total = 0;
        for (int intValue : intValues) {
            total = total + intValue;
        }
        //初始化数据
        List<SliceValue> values = new ArrayList<SliceValue>();
        if (total == 0) {
            for (int i = 0; i < numValues; ++i) {
                SliceValue sliceValue = new SliceValue(1, Color.parseColor(colors[i]));
                values.add(sliceValue);
            }
        } else {
            for (int i = 0; i < numValues; ++i) {
                SliceValue sliceValue = new SliceValue((float) intValues[i], Color.parseColor(colors[i]));
                values.add(sliceValue);
            }
        }
        data = new PieChartData(values);
        data.setHasLabels(false);
        data.setHasLabelsOnlyForSelected(false);
        data.setHasLabelsOutside(false);
        data.setHasCenterCircle(true);
        data.setCenterCircleScale(0.5f);
        data.setSlicesSpacing(0);
        chart.setPieChartData(data);
    }

    private void initFloatPerChart(PieChartView chart, double h9AddAction, double mp70AddAction, double zhibeijiAction, double h9Action, double mp70Action) {
        PieChartData data;
        int numValues = 5;
        String[] colors = {"#FFF2C1F0", "#FFAEDAFF", "#FFFFC879", "#FF9BEDB8", "#FFFFDCBC"};
        double[] intValues = {h9AddAction, mp70AddAction, zhibeijiAction, h9Action, mp70Action};
        double total = 0;
        for (double intValue : intValues) {
            total = total + intValue;
        }
        //初始化数据
        List<SliceValue> values = new ArrayList<SliceValue>();
        if (total == 0) {
            for (int i = 0; i < numValues; ++i) {
                SliceValue sliceValue = new SliceValue(1, Color.parseColor(colors[i]));
                values.add(sliceValue);
            }
        } else {
            for (int i = 0; i < numValues; ++i) {
                SliceValue sliceValue = new SliceValue( (float) intValues[i], Color.parseColor(colors[i]));
                values.add(sliceValue);
            }
        }
        data = new PieChartData(values);
        data.setHasLabels(false);
        data.setHasLabelsOnlyForSelected(false);
        data.setHasLabelsOutside(false);
        data.setHasCenterCircle(true);
        data.setCenterCircleScale(0.5f);
        data.setSlicesSpacing(0);
        chart.setPieChartData(data);
    }


    public void setListener(CountChartActionClose listener) {
        countChartOtherClose = listener;
    }

    public void setTitle(String title) {
        if (tvTitle != null) tvTitle.setText(title);
    }

    public interface CountChartActionClose {

        void loadTrandMonthCharData(String month);
    }

    private void formatFloat(TextView view, double value, String danwei) {
        if (value >= 10000) {
            if (value >= 100000000) {
                double n = value / 100000000;

                view.setText( StringUtil.INSTANCE.calculateProfit(n) + "亿"+danwei);
            } else {
                double n = value / 10000;

                    view.setText( StringUtil.INSTANCE.calculateProfit(n) + "万"+danwei);
            }
        } else {
                view.setText( StringUtil.INSTANCE.calculateProfit(value)+danwei);
        }
    }

    private void formatInt(TextView view, int value, String danwei) {
        if (value >= 10000) {
            if (value >= 100000000) {
                double n = value / 100000000;
                view.setText( StringUtil.INSTANCE.calculateProfit(n) +"亿"+ danwei);
            } else {
                double n = value / 10000;
                view.setText( StringUtil.INSTANCE.calculateProfit(n) +"万" +danwei);
            }
        } else {
            view.setText( value + danwei);
        }
    }
}
